package gwt.bbsbot.server.datastore;

import gwt.bbsbot.server.entity.QuestionDataEntity;
import gwt.bbsbot.server.pmf.PMF;
import gwt.bbsbot.server.twitter.beans.TweetBean;

import java.util.List;

import javax.jdo.PersistenceManager;

public class DataStoreUtil {

	/**
	 * 質問データを取得する
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<QuestionDataEntity> getQuestionData10(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		//TODO 件数指定に変える
		String query = "select from " + QuestionDataEntity.class.getName();

		List<QuestionDataEntity> datas = (List<QuestionDataEntity>) pm
				.newQuery(query).execute();
		return datas;

	}
	
	/**
	 * TweetBeanを使って質問を投稿する
	 * @param bean
	 */
	public static void insertQuestionDataByTweetBean(TweetBean bean){
		QuestionDataEntity data = new QuestionDataEntity(
														bean.getScreen_name(),
														bean.getText(),
														bean.getProfileImageURL(),
														bean.getWhoami()
														);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(data);
		}catch(Exception e){
		}finally {
			pm.close();
		}
	}
	
	
	public static boolean isExistQuestionData(TweetBean bean){
		List<QuestionDataEntity> list = getQuestionData10();
		for(QuestionDataEntity entity : list){
			if( entity.getName().equals(bean.getScreen_name()) 
					&& entity.getQuestionData().equals(bean.getText())
//					&& entity.getGenre().equals(bean.getWhoami())
			){
//				System.out.println("----------------------------");
//				System.out.print("f :");
//				System.out.println(entity.toString());
//				System.out.println("   " + bean.toString());
				return true;
			}
		}
		System.out.println("----------------------------");
		System.out.print("t :");
		System.out.println(bean.toString());
		return false;
	}
	
}
